/**
 * 溶解氧电极液
 */
Balance = Widget.extend({
	runningAction:null,
	ctor:function(){
		this._super();
		this.init();
		this.setTag(TAG_BALANCE_ALL);
		this.initAction();
	},
	init:function(){
		// 电子天平
		this.body = new Button(this, 10, TAG_BALANCE, "#Balance1.png", this.callback,this);
		// 气泡
		var bubble=new Button(this,10,TAG_BUBBLE,"#Bubble.png",this.callback,this);
		bubble.setPosition(-2,-this.body.height*0.43);
		bubble.setVisible(false);
		// 左边旋钮
		var knob=new Button(this,10,TAG_KNOB,"#Knob.png",this.callback,this);
		knob.setPosition(-this.body.width*0.2,-this.body.height*0.46);
		knob.setVisible(false);
		// 右边旋钮
		var knob1=new Button(this,10,TAG_KNOB1,"#Knob.png",this.callback,this);
		knob1.setPosition(this.body.width*0.16,-this.body.height*0.46);
		knob1.setVisible(false);
		// 气泡特写
		var zero=new Button(this,10,TAG_ZERO,"#Zero/Zero1.png",this.callback,this);
		zero.setPosition(-this.body.width*0.65,-this.body.height*0.4);
		zero.setOpacity(0);
		zero.setScale(0.5);
		// 电源线
		var powerLine=new Button(this,9,TAG_POWERLINE,"#PowerLine1.png",this.callback,this);
		powerLine.setPosition(-this.body.width*0.98,this.body.height*0.3);
		// 插座
		var outlet=new Button(this,8,TAG_OUTLET,"#outlet.png",this.callback,this);
		outlet.setPosition(-this.body.width*0.77,this.body.height*0.72);
		// 开机按钮
		var boot=new Button(this,11,TAG_BOOT,"#boot.png",this.callback,this);
		boot.setPosition(-this.body.width*0.23,-this.body.height*0.4);
		boot.setVisible(false);
		// 调零按钮
		var tare=new Button(this,11,TAG_TARE,"#tare.png",this.callback,this);
		tare.setPosition(-this.body.width*0.362,-this.body.height*0.365);
		tare.setVisible(false);
		// 显示器显示数值
		var num=new Button(this,11,TAG_NUM,"#num.png",this.callback,this);
		num.setPosition(0,-this.body.height*0.347);
		
		var door=new Button(this,11,TAG_DOOR,"#door.png",this.callback,this);
		door.setPosition(this.body.width*0.45,-this.body.height*0.05);
	},
	initAction:function(){
		var animFrames=[];
		for(var i=1;i<6;i++){
			var str="Balance"+i+".png";
			var frame=cc.spriteFrameCache.getSpriteFrame(str);
			animFrames.push(frame);
		}
		var animation=new cc.Animation(animFrames,0.05);
		this.runningAction=cc.repeat(cc.animate(animation),1);
		this.runningAction.retain();
	},
	callback:function(p){
		var func=cc.callFunc(function(){
			gg.flow.next();
		},this);
		var action=gg.flow.flow.action;
		var knob=this.getChildByTag(TAG_KNOB);
		var knob1=this.getChildByTag(TAG_KNOB1);
		var zero=this.getChildByTag(TAG_ZERO);
		var bubble=this.getChildByTag(TAG_BUBBLE);
		var powerLine=this.getChildByTag(TAG_POWERLINE);
		var boot=this.getChildByTag(TAG_BOOT);
		var num=this.getChildByTag(TAG_NUM);
		var tare=this.getChildByTag(TAG_TARE);
		var fadein=cc.fadeIn(1);
		var fadeout=cc.fadeOut(1);
		switch(p.getTag()){
		case TAG_POWERLINE:
			p.runAction(cc.sequence(cc.callFunc(function() {
				p.setSpriteFrame("PowerLine.png");
			},this),func));
			break;
		case TAG_BALANCE:
			if(action==ACTION_DO1){
				p.runAction(cc.sequence(cc.callFunc(function() {
					this.body.setSpriteFrame("Balance_back.png");
					num.setVisible(false);
					knob.setVisible(true);
					knob1.setVisible(true);
					bubble.setVisible(true);
					powerLine.setVisible(false);
				},this),func));
			}else if(action==ACTION_DO2){
				p.runAction(cc.sequence(cc.callFunc(function() {
					p.setSpriteFrame("Balance1.png");
					num.setVisible(true);
					powerLine.setVisible(true);
					knob.setVisible(false);
					knob1.setVisible(false);
					bubble.setVisible(false);
					boot.setVisible(true);
				},this),func));
			}
			break;
		case TAG_BUBBLE:
			p.runAction(cc.sequence(cc.callFunc(function() {
				zero.runAction(cc.sequence(fadein,cc.delayTime(1),fadeout));
			},this),cc.delayTime(3),func));
			break;
		case TAG_KNOB:
			p.runAction(cc.sequence(cc.callFunc(function() {
				zero.setSpriteFrame("Zero/Zero.png");
				zero.runAction(cc.sequence(fadein,cc.delayTime(1),fadeout));
			},this),cc.delayTime(3),func));
			break;
		case TAG_KNOB1:
			p.runAction(cc.sequence(cc.callFunc(function() {
				zero.setSpriteFrame("Zero/Zero2.png");
				zero.runAction(cc.sequence(fadein,cc.delayTime(1),fadeout));
			},this),cc.delayTime(3),func));
			break;
		case TAG_BOOT:
			if(action==ACTION_DO1){
				p.runAction(cc.sequence(cc.callFunc(function() {
					num.setSpriteFrame("num.png");
					this.body.setVisible(false);
					num.setVisible(false);
					boot.setVisible(false);
					tare.setVisible(false);
					powerLine.setVisible(false);
				},this),func));
			}else{
				p.runAction(cc.sequence(cc.callFunc(function() {
					num.setSpriteFrame("num2.png");
					ll.run.clock.doing();
				},this),cc.delayTime(4),cc.callFunc(function() {
					ll.run.clock.point.stopAllActions();
					ll.run.clock.showTime();
				},this),cc.delayTime(1),cc.callFunc(function() {
					ll.run.clock.stop();
					tare.setVisible(true);
				},this),func));
			}
			break;
		case TAG_TARE:
			p.runAction(cc.sequence(cc.callFunc(function() {
				num.setSpriteFrame("num1.png");
			},this),func));
			break;
		case TAG_DOOR:
			if(action==ACTION_DO1||action==ACTION_DO3||action==ACTION_DO5||action==ACTION_DO7){
				this.body.runAction(cc.sequence(this.runningAction,func));
			}else if(action==ACTION_DO2||action==ACTION_DO4||action==ACTION_DO6||action==ACTION_DO8){
				this.body.runAction(cc.sequence(this.runningAction.reverse(),func));
			}
			break;
		default:
			break;
		}
	}
});